package mashuaiproject.mashuaiproject.controller;

import mashuaiproject.mashuaiproject.dto.CommentDto;
import mashuaiproject.mashuaiproject.dto.ResultDto;
import mashuaiproject.mashuaiproject.exption.CustomizeCode;
import mashuaiproject.mashuaiproject.mapper.CommentMapper;
import mashuaiproject.mashuaiproject.model.CommentSon;
import mashuaiproject.mashuaiproject.model.Comments;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.serivce.CommentSerivce;
import mashuaiproject.mashuaiproject.serivce.JMontiorSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommentContrller {
    @Autowired
    private CommentSerivce commentSerivce;
    @Autowired
    private JMontiorSerivce jMontiorSerivce;
    @ResponseBody
    @PostMapping("/comments")  //@RequestBody CommentDto commentDto自动将请求的参数赋值给commentDto的每个属性
    public  Object post( @RequestBody CommentDto commentDto,HttpServletRequest request){
        //获取session1的信息
        Users users= (Users) request.getSession().getAttribute("user");
        if(users==null){
            return ResultDto.erroOf(CustomizeCode.NO_LOGIN);
        }
        //内容不为空
        if(commentDto==null || StringUtils.isEmpty(commentDto.getContent())){
            return ResultDto.erroOf(CustomizeCode.COUNT_IS_EMPTY);
        }
        Comments comments=new Comments();
        comments.setParent_id(commentDto.getParent_id());//这个parent_id是值文章的id
        comments.setContent(commentDto.getContent());
        comments.setType(commentDto.getType());
        comments.setGmt_create(System.currentTimeMillis());
        comments.setGmt_modified(comments.getGmt_create());
        comments.setCommentator(users.getId());
        //插入评论
        commentSerivce.insertComment(comments);
        Map<Object,Object> objectObjectMap=new HashMap<>();
        objectObjectMap.put("message","成g");
        jMontiorSerivce.checkHasFristFatie(users.getId(),"comment");
        jMontiorSerivce.Upleave(users.getId());
        return ResultDto.okOf();
    }
    //二级评论
    @ResponseBody
    @PostMapping ("/commentSon")
    public Map<String ,Object>  comments(@RequestBody CommentSon commentSon
                                , HttpServletRequest request){
        Map<String ,Object> map=new HashMap<>();
        //获取到session的用户
        Users users= (Users) request.getSession().getAttribute("user");
        if(users==null){
            map.put("code","2004");
            return map;
        }
        //将数据插入到二级评论
        commentSon.setGmt_create(System.currentTimeMillis());
        commentSon.setGmt_modified(commentSon.getGmt_create());
        commentSon.setUser_id(users.getId());
        System.err.println(commentSon.toString());
        //调用serivce插入数据
         int commentSonList=commentSerivce.insertCommentSon(commentSon);//插入数据
        //更新评论数量
        int commentSonCount=commentSerivce.updateCommentSonCount(commentSon.getFather_id());//更新耳机评论的数量,并且返回的事当前的评论数量

        List<Object> listArry=new ArrayList<>();
        listArry.add(commentSon);
        listArry.add(users);
        listArry.add(commentSonCount);
        map.put("listArry",listArry);//将当前信息携带过去

        return map;
    }

}
